package com.bwie.controller;

import cn.hutool.core.date.DateUtil;
import com.bwie.pojo.Result;
import com.bwie.pojo.po.UserPo;
import com.bwie.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ 用户控制层
 * @descriptions: UserController
 * @author: LiYang
 * @date: 2025/3/14 10:13
 * @version: 1.0
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**\
     * 去登录页面
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 发送验证码
     * @param userPhone
     * @return
     */
    @RequestMapping("getSms")
    @ResponseBody
    public Result getSms(String userPhone){
        return userService.getSms(userPhone);
    }

    /**
     * 登录方法
     * @param userPo
     * @return
     */
    @RequestMapping("getSmsLogin")
    @ResponseBody
    public Result getSmsLogin(@RequestBody UserPo userPo){
        return userService.getSmsLogin(userPo);
    }

    /**
     * 获得医生下拉框
     * @param
     * @return
     */
    @RequestMapping("getDoctor")
    @ResponseBody
    public Result getDoctor(){
        return userService.getDoctor();
    }
    /**
     * 获得性别下拉框
     * @param
     * @return
     */
    @RequestMapping("getSex")
    @ResponseBody
    public Result getSex(){
        return userService.getSex();
    }
    /**
     * 获得科室下拉框
     * @param
     * @return
     */
    @RequestMapping("getDep")
    @ResponseBody
    public Result getDep(){
        return userService.getDep();
    }
    /**
     * 根据名字回显相关数据
     * @param
     * @return
     */
    @RequestMapping("selectPatientName")
    @ResponseBody
    public Result selectPatientName( String patientName){
        return userService.selectPatientName(patientName);
    }
    /**
     * 根据身份证计算年龄
     * @param
     * @return
     */
    @RequestMapping("selectIdCard")
    @ResponseBody
    public Result selectIdCard(String idCard) throws ParseException {
      if(StringUtils.isEmpty(idCard)){
          return Result.error("身份证号码为空");
      }
      //140724 2006-08-30 0105
      //012345 67890123 4567
        String card = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(card);
        Date nowDate = new Date();
        long age = DateUtil.betweenYear(parse, nowDate, true);
        return Result.success("计算成功",age);
    }
}
